import pyforest
from django.shortcuts import render,redirect
from urllib import request
from django.shortcuts import render
#连接mysql的类
import pymysql
#引入项目的settings文件
from django.conf import settings

def execute_open():
    import winrm
    import pymssql
    #查看最新一条生产数据是在生产哪一个工单
    sql_server_conn = pymssql.connect(host='192.168.118.128', user='barcode', password='barcodefornexcom',
                                      database='MIS')

    sql_server_cursor_OrderNumber = sql_server_conn.cursor()
    sql_server_cursor_OrderNumber.execute("select top 1 form_no from maf_master order by create_date desc ")
    now_order_number = list(sql_server_cursor_OrderNumber.fetchone())[0]
    print(now_order_number)



    #查询这个工单机种名称
    sql_server_conn_NEXCOM = pymssql.connect(host='192.168.118.128', user='barcode', password='barcodefornexcom',
                                             database='NEXCOM')
    sql_server_conn_NEXCOM_ProductionName = sql_server_conn_NEXCOM.cursor()
    sql_server_conn_NEXCOM_ProductionName.execute("select TA034 from MOCTA where TA002 = '%s';" % (now_order_number))
    now_production_name = list(sql_server_conn_NEXCOM_ProductionName.fetchone())[0]
    print(now_production_name)


    #展示图片的路径
    aaaaaa = now_production_name+".jpg"


    print(aaaaaa)
    a = list()
    a.append(now_order_number)
    a.append(now_production_name)
    a.append(aaaaaa)
    print(a)


    #远程执行CMD命令
    import os
    path = r"C:\Users\Administrator\PycharmProjects\changer_mes\static\bat\%s.bat" % (now_production_name,)
    os.system('schtasks /run /s 10.130.1.161 /u Administrator /p 111 /tn  %s ' % (now_production_name))


execute_open()

